package org.codeart.juc.visible;

public class VolatileDemo {

    // 不使用 volatile 修饰变量的话, 主线程对 flag 操作对其他线程不可见
    public static volatile boolean flag = true;
    
    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            while (flag) {
                // ...
            }
            System.out.println("t1 thread ends");
        }, "t1").start();
        Thread.sleep(100);
        flag = false;
        System.out.println("main thread sets flag false");
    }

}
